package com.campusnews.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.campusnews.mapper.InvitationMapper;
import com.campusnews.mapper.SchoolUserMapper;
import com.campusnews.mapper.TopicMapper;
import com.campusnews.pojo.Invitation;
import com.campusnews.pojo.SchoolUser;
import com.campusnews.pojo.Topic;
import com.campusnews.pojo.dayData;
import com.campusnews.service.DataVisualService;
import com.campusnews.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DataVisualServiceImpl implements DataVisualService {
    @Autowired
    private SchoolUserMapper schoolUserMapper;
    @Autowired
    private InvitationMapper invitationMapper;
    @Autowired
    private TopicMapper topicMapper;

    @Override
    public Map<String,Integer> countTotalNum() {
        Map<String, Integer> map = new HashMap<>();
        QueryWrapper<SchoolUser> wrapperUser = new QueryWrapper<>();
        QueryWrapper<Topic> wrapperTopic = new QueryWrapper<>();
        QueryWrapper<Invitation> wrapperInvite = new QueryWrapper<>();
        Integer userCount = schoolUserMapper.selectCount(wrapperUser);
        map.put("userTotal",userCount);
        Integer userTodayCount = schoolUserMapper.getTodayUserNum();
        map.put("userTodayCount",userTodayCount);
        Integer TopicCount = topicMapper.selectCount(wrapperTopic);
        map.put("TopicTotal",TopicCount);
        Integer inviteCount = invitationMapper.selectCount(wrapperInvite);
        map.put("inviteTotal",inviteCount);
        return map;
    }

    @Override
    public List<dayData> getSevenDayInviteNum() {
        List<Map<String, Object>> map = null;
        try {
            map = invitationMapper.getSevenDayInviteNum();
        } catch (Exception e) {
            e.printStackTrace();
        }
        ArrayList<Integer> list = DateUtils.test(7);
        List<dayData> invites = new ArrayList<>();
        if (map !=null){
            for (Integer integer : list) {
                int tag = 0;
                for (Map<String, Object> objectMap : map) {
                    if (objectMap.get("DAY(create_time)") == integer){
                        Object num = objectMap.get("count(DAY(create_time))");
                        dayData invite = new dayData(integer,num);
                        invites.add(invite);
                        tag = 1;
                        break;
                    }
                }
                if (tag ==0 ){
                    dayData invite = new dayData(integer,0);
                    invites.add(invite);
                }
            }
        }else {
            for (Integer integer : list) {
                dayData invite = new dayData(integer,0);
                invites.add(invite);
            }
        }

        System.out.println(invites.toString());

        return invites;
    }

    @Override
    public List<dayData> getSevenDayUserNum() {
        List<Map<String, Object>> map = null;
        try {
            map = schoolUserMapper.getSevenDayUserNum();
        } catch (Exception e) {
            e.printStackTrace();
        }
        ArrayList<Integer> list = DateUtils.test(7);
        List<dayData> Users = new ArrayList<>();
        if (map!=null){
            for (Integer integer : list) {
                int tag = 0;
                for (Map<String, Object> objectMap : map) {
                    if (objectMap.get("DAY(create_time)") == integer){
                        Object num = objectMap.get("count(DAY(create_time))");
                        dayData user = new dayData(integer,num);
                        Users.add(user);
                        tag = 1;
                        break;
                    }
                }
                if (tag ==0 ){
                    dayData user = new dayData(integer,0);
                    Users.add(user);
                }
            }

            System.out.println(Users.toString());
        }else {
            for (Integer integer : list) {
                dayData topic = new dayData(integer,0);
                Users.add(topic);
            }
        }
        return Users;
    }

    @Override
    public List<dayData> getSevenDayTopicNum() {
        List<Map<String, Object>> map = null;
        try {
            map = topicMapper.getSevenDayTopicNum();
        } catch (Exception e) {
            e.printStackTrace();
        }
        ArrayList<Integer> list = DateUtils.test(7);
        List<dayData> topics = new ArrayList<>();
        if (map!=null){
            for (Integer integer : list) {
                int tag = 0;
                for (Map<String, Object> objectMap : map) {
                    if (objectMap.get("DAY(create_time)") == integer){
                        Object num = objectMap.get("count(DAY(create_time))");
                        dayData topic = new dayData(integer,num);
                        topics.add(topic);
                        tag = 1;
                        break;
                    }
                }
                if (tag ==0 ){
                    dayData topic = new dayData(integer,0);
                    topics.add(topic);
                }
            }

            System.out.println(topics.toString());
        }else {
            for (Integer integer : list) {
                dayData topic = new dayData(integer,0);
                topics.add(topic);
            }
        }
        return topics;
    }
}
